package com.nosnow.gmall.list.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nosnow.gmall.bean.BaseAttrInfo;
import com.nosnow.gmall.bean.BaseAttrValue;
import com.nosnow.gmall.bean.SkuLsParams;
import com.nosnow.gmall.bean.SkuLsResult;
import com.nosnow.gmall.service.ListService;
import com.nosnow.gmall.service.ManageService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author bingheng
 */

@Controller
public class ListController {

    @Reference
    private ManageService manageService;

    @Reference
    private ListService listService;

    @RequestMapping("list.html")
    public String getList(SkuLsParams skuLsParams, HttpServletRequest httpServletRequest) {
        skuLsParams.setPageSize(4);
        //根据参数返回sku列表
        SkuLsResult skuLsResult = listService.search(skuLsParams);
        List<String> attrValueIdList = skuLsResult.getValueIdList();
        List<BaseAttrInfo> attrList = manageService.getAttrList(attrValueIdList);

        // 已选择的属性值列表
        List<BaseAttrValue> attrValueSelectedList = new ArrayList<>();
        String urlParam = makeUrlParam(skuLsParams);
        for (Iterator<BaseAttrInfo> iterator = attrList.iterator(); iterator.hasNext(); ) {
            BaseAttrInfo baseAttrInfo = iterator.next();
            List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
            for (BaseAttrValue baseAttrValue : attrValueList) {

                baseAttrValue.setUrlParam(urlParam);

                if(skuLsParams.getValueId() != null && skuLsParams.getValueId().length > 0) {
                    for(String valueId : skuLsParams.getValueId()) {

                        // 选中条件的属性值和查询结果的属性值
                        if(valueId.equals(baseAttrValue.getId())) {
                            iterator.remove();

                            // 构造面包屑列表
                            BaseAttrValue baseAttrValueSelected = new BaseAttrValue();
                            baseAttrValueSelected.setValueName(baseAttrInfo.getAttrName() + ":" + baseAttrValue.getValueName());
                            baseAttrValueSelected.setUrlParam(makeUrlParam(skuLsParams, valueId));
                            attrValueSelectedList.add(baseAttrValueSelected);
                        }
                    }
                }
            }

        }

        int totalPages = (int)(skuLsResult.getTotal() + skuLsParams.getPageSize() - 1) / skuLsParams.getPageSize();


        // 总页数
        httpServletRequest.setAttribute("totalPages", totalPages);
        // 当前页
        httpServletRequest.setAttribute("pageNo", skuLsParams.getPageNo());
        // 路径
        httpServletRequest.setAttribute("urlParam", urlParam);


        httpServletRequest.setAttribute("keyword", skuLsParams.getKeyword());
        httpServletRequest.setAttribute("attrValueSelectedList", attrValueSelectedList);
        httpServletRequest.setAttribute("attrList", attrList);
        httpServletRequest.setAttribute("skuLsInfoList", skuLsResult.getSkuLsInfoList());
        return "list";
    }

    private String makeUrlParam(SkuLsParams skuLsParams, String ...excludeValueIds) {
        String urlParam = "";

        if(skuLsParams.getKeyword() != null) {
            urlParam += "keyword" + skuLsParams.getKeyword();
        }

        if(skuLsParams.getCatalog3Id() != null) {
            if(urlParam.length() > 0) {
                urlParam += "&";
            }
            urlParam += "catalog3Id" + skuLsParams.getCatalog3Id();
        }

        // 构造属性值参数
        if(skuLsParams.getValueId() != null && skuLsParams.getValueId().length > 0) {
            for(int i = 0; i < skuLsParams.getValueId().length; i++) {
                String valueId = skuLsParams.getValueId()[i];
                if(excludeValueIds != null && excludeValueIds.length > 0) {
                    String excludeValueId = excludeValueIds[0];
                    if(excludeValueId.equals(valueId)) {
                        continue;
                    }
                }
                if(urlParam.length() > 0) {
                    urlParam += "&";
                }

                urlParam += "valueId" + valueId;
            }
        }

        return urlParam;
    }
}
